package d8_thread_pool;

import java.util.concurrent.*;

public class ThreadPoolExecutorDemo02 {
    public static void main(String[] args) {
        //线程池: 处理Callable任务
        //1、创建线程池
        ExecutorService pool = new ThreadPoolExecutor(3,
                5,
                1,
                TimeUnit.MINUTES,
                new ArrayBlockingQueue<>(3),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.CallerRunsPolicy());

        //2、处理任务
        //利用ExecutorService的实现类对象,调用submit()-->返回一个未来任务对象,然后再使用未来任务对象,调用get()方法获取结果
        //这是别人底层设计好的,都要记忆
        //只能一个一个的去new
//        FutureTask<String> task = new FutureTask<>(new MyCallable(1));
        Future<String> f1 = pool.submit(new MyCallable(100));
        Future<String> f2 = pool.submit(new MyCallable(200));
        Future<String> f3 = pool.submit(new MyCallable(300));


        //获取结果
        //代用get()会出现异常,所以要处理异常
        try {
            String s = f1.get();
            //进行结果的输出
            System.out.println(s);
        } catch (Exception e) {
           e.printStackTrace();
        }
        try {
            String s = f2.get();
            System.out.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        }
        try {
            String s = f3.get();
            System.out.println(s);
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
